package com.example.zhu.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.zhu.entity.AddressBook;
import com.example.zhu.dto.EditAddressDTO;
import com.example.zhu.vo.UserVo;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface AddressBookMapper extends BaseMapper<AddressBook> {
    @Update("update users t1,address_book t2 set t1.type = #{type},t2.phone = #{phone},t2.address = #{address} " +
            "where t1.id = t2.address_id and t1.id = #{id}")
    void update(EditAddressDTO editAddressDTO);

    @Select("select t1.openid,t1.type,t2.id,t2.address,t2.phone from users t1,address_book t2 where t1.openid = t2.openid and t2.id = #{id} and t2.is_default = 1 ")
    UserVo getDA(Integer id);

    @Select("select * from address_book")
    List<AddressBook> selectAll();

    @Delete("delete from address_book where address_id = #{id}")
    void deleteAddress(Integer id);

    @Select("select t1.openid,t1.type,t2.id,t2.address,t2.phone from users t1,address_book t2 where t1.openid = t2.openid and t2.phone like concat('%',#{phone},'%') and t2.is_default = 1")
    List<UserVo> getByPhone(String phone);

    @Select("select t1.openid , t2.address from users t1,address_book t2 where t1.openid = t2.openid and t2.is_default = 1")
    List<UserVo> selectAllUsers();

    @Select("select address , phone from address_book where openid=#{openid} and is_default=1")
    AddressBook selectByOpenid(String openid);
}
